package com.example.project22_editingmediaplayer.adapter;

import java.util.ArrayList;
import java.util.Random;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;

import com.example.project22_editingmediaplayer.R;
import com.example.project22_editingmediaplayer.adapter.RowSong.RowHolder;
import com.example.project22_editingmediaplayer.objects.SongObject;
import com.example.project22_editingmediaplayer.objects.VideoObject;
import com.example.project22_editingmediaplayer.resource.SongResource;

public class RowVideo extends BaseAdapter {
	public ArrayList<VideoObject> listData;
	private LayoutInflater mInflater;
	public final ArrayList<Integer> listselected;

	public RowVideo(ArrayList<VideoObject> listData, Context mContext) {
		this.listData = listData;
		mInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		listselected = new ArrayList<Integer>();
	}

	public void resetData(ArrayList<VideoObject> listData) {
		this.listData = listData;
		this.notifyDataSetChanged();
	}

	public void addData(VideoObject videoObject) {
		listData.add(videoObject);
		this.notifyDataSetChanged();
	}

	public ArrayList<VideoObject> getAllData() {
		return this.listData;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		try {
			if (!listData.isEmpty())
				return listData.size();
			else
				return 0;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return 0;
	}
	@Override
	public Object getItem(int postition) {
		// TODO Auto-generated method stub
		return listData.get(postition);
	}

	@Override
	public long getItemId(int postition) {
		// TODO Auto-generated method stub
		return postition;
	}

	@Override
	public View getView(final int postition, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		RowHolder holder = null;
		View view = convertView;
		if (view == null) {
			view = mInflater.inflate(R.layout.row_item, null);
			holder = new RowHolder();
			holder.imvAvarSong = (ImageView) view
					.findViewById(R.id.imvAvarSong);
			holder.txvSong = (TextView) view.findViewById(R.id.txtNameSong);
			holder.txvArtis = (TextView) view.findViewById(R.id.txtArtis);
			view.setTag(holder);
		} else {
			holder = (RowHolder) view.getTag();
		}
		final VideoObject videoObject = listData.get(postition);
		holder.txvSong.setText(videoObject.get_name());
		holder.txvArtis.setText(videoObject.get_artist());
		if (videoObject.get_bitmap() != null) {
			holder.imvAvarSong.setImageBitmap(videoObject.get_bitmap());
			holder.imvAvarSong.setScaleType(ScaleType.CENTER);
		}else
		{
			Random rand = new Random();
			int currentSongIndex = rand.nextInt(SongResource.musicBackGround.length);
			holder.imvAvarSong.setBackgroundResource(SongResource.musicBackGround[currentSongIndex]);
		}
		// set image view
		return view;
	}

	public static final class RowHolder {
		public ImageView imvAvarSong;
		public TextView txvSong;
		public TextView txvArtis;
	}
}

